package com.example.simple_cheat;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<MSG> meslist = new ArrayList<>();
    private EditText editText;
    private Button send;
    private Button back;
    private RecyclerView recyclerView;
    private Myadapter myadapter;
    private String f_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar acbar = getSupportActionBar();
        if(acbar != null){
            acbar.hide();
        }


        Intent intent = getIntent();
        f_name = intent.getStringExtra("name");

        TextView title = (TextView)findViewById(R.id.title_text);
        title.setText(f_name);


        initmessage();
        editText = (EditText)findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        back = (Button)findViewById(R.id.back_button);
        recyclerView = (RecyclerView)findViewById(R.id.recycle_view);
        myadapter = new Myadapter(meslist);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myadapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
                Date date = new Date(System.currentTimeMillis());
                String str=simpleDateFormat.format(date);
                String Content = editText.getText().toString();
                if(!"".equals(Content)){
                    message_in_database newmess = new message_in_database();
                    newmess.setData(str);
                    newmess.setUserID("未完成");
                    newmess.setMessage(Content);
                    newmess.setName(f_name);
                    newmess.setType(1);
                    newmess.save();

                    MSG msg = new MSG(Content,MSG.Type_sent,f_name,str);
                    meslist.add(msg);
                    myadapter.notifyItemInserted(meslist.size()-1);
                    recyclerView.scrollToPosition(meslist.size()-1);
                    editText.setText("");
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MainActivity.this.finish();

            }
        });

    }

    public void initmessage(){
        meslist.clear();
        List<message_in_database> list_for_databases = LitePal.findAll(message_in_database.class);
        MSG first = new MSG("kakakakaka",0,"woodx","10-19");
        meslist.add(first);
        for(message_in_database t : list_for_databases){
            if(f_name.equals(t.getName())){
                String tempstr = t.getMessage();
                int type  = t.getType();
                String tempname = t.getName();
                MSG tempmsg = new MSG(tempstr,type,tempname,t.getData());
                meslist.add(tempmsg);
            }
        }
    }

}